<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import router from '../router';

const route= useRoute()

const gameList =  ref<any[]>([])
const activeGame = ref<any>({})
const messages = ref<any>({})

const editor = ref({
  show:false,
  fromAdd:false,
  openDir:false,
  errorMsg:{
    gameMsg:"",
    locationMsg:"",
  }
})

const editGameInfo = ref<any>({})

function save(){
  window.gamerfinger.triggerSave(JSON.stringify(activeGame.value)).then(data=> activeGame.value = data)
}

function load(){
  console.log("triggerLoad",activeGame.value)
  window.gamerfinger.triggerLoad(JSON.stringify(activeGame.value)).then(data=> activeGame.value = data)
}

function selectFile(){
  window.gamerfinger.selectFile(editor.value.openDir).then(data=>{
    if(data.canceled){
      return 
    }
    const path = data.filePaths[0]
    editGameInfo.value.from = path
  })
}

function editGame(){
  if(editor.value.fromAdd){
    const exist = gameList.value.find(d => d.game == editGameInfo.value.game)
    if(exist != undefined){
      editor.value.errorMsg.gameMsg = messages.value.sl_game_msg
      return
    }
  
    if(editGameInfo.value.game==undefined || editGameInfo.value.game==null || editGameInfo.value.game.length < 1){
      editor.value.errorMsg.gameMsg = messages.value.sl_game_msg
      return
    }
  }

  if(editGameInfo.value.from==undefined || editGameInfo.value.from==null || editGameInfo.value.from.length < 1){
    editor.value.errorMsg.locationMsg = messages.value.sl_location_msg
    return
  }
  
  window.gamerfinger.editSL(JSON.stringify(editGameInfo.value)).then(data=>{
    gameList.value = data
    let game= gameList.value.find(d => d.game == activeGame.value.game)
    if(game != undefined){
      activeGame.value = game
    }
    game= gameList.value.find(d=>d.game == editGameInfo.value.game)
    if(game != undefined){
      editGameInfo.value = game
    }
    editor.value.show = false
  })
}

function loadGames(){
  window.gamerfinger.getSL().then(data=>{
    gameList.value = data
    let game= gameList.value.find(d=>d.game == activeGame.value.game)
    if(game != undefined){
      activeGame.value = game
    }
    game= gameList.value.find(d=>d.game == editGameInfo.value.game)
    if(game != undefined){
      editGameInfo.value = game
    }
  })
}

function changeActive(game:any){
  activeGame.value = game
}

function goHome(){
  router.push({
    name:"home",
  })
}

function openEdit(data:any){
  editGameInfo.value  = data
  editor.value.show = true
  editor.value.fromAdd=false
  editor.value.errorMsg.gameMsg = ""
  editor.value.errorMsg.locationMsg = ""
}

function openAdd(){
  editGameInfo.value  = {}
  editor.value.show = true
  editor.value.fromAdd=true
  editor.value.errorMsg.gameMsg = ""
  editor.value.errorMsg.locationMsg = ""
}

onMounted(()=>{
  if(route.meta.needSave){
    window.gamerfinger.saveOpen(route.name as string)
  }
  loadGames()
  window.gamerfinger.getLanguage().then(data=>{
    messages.value = data
  })
})

</script>

<template>
  <div class="sl">
    <section class="gamelist-glass">
      <h1 class="sidebar-title">{{messages.sl || 'SL管理'}}</h1>
      <div class="game-list">
        <div v-for="game in gameList"  
          :class="{
            'game-item':true,
            'game-active': (game.game == activeGame.game)
          }" 
          @click="changeActive(game)">
          {{ game.game }}
        </div>
      </div>
      <div @click="goHome" class="back-button">
        <i class="fas fa-arrow-left"></i>
        <span>{{messages.back || '返回'}}</span>
      </div>
    </section>
    
    <main class="main-content">
      <div class="panel-glass" v-if="activeGame.game!=undefined && activeGame.game!=''">
        <h1 class="panel-title">{{ activeGame.game }}</h1>
        <div class="info-section">
          <div class="info-item">
            <span class="info-label"><i class="fas fa-folder"></i> 源地址：</span>
            <span class="info-value">{{activeGame.from}}</span>
          </div>
          <div class="info-item">
            <span class="info-label"><i class="fas fa-folder-open"></i> 目标地址：</span>
            <span class="info-value">./config/sl/{{activeGame.to}}/</span>
          </div>
          <div class="info-item">
            <span class="info-label"><i class="fas fa-clock"></i> 上次备份时间：</span>
            <span class="info-value">{{ activeGame.last }}</span>
          </div>
        </div>
        <div class="action-buttons">
          <button class="action-btn edit-btn" @click="openEdit(activeGame)">
            <i class="fas fa-edit"></i> {{messages.sl_e || '编辑'}}
          </button>
          <button class="action-btn save-btn" @click="save">
            <i class="fas fa-save"></i> {{messages.sl_s || '保存'}}
          </button>
          <button class="action-btn load-btn" @click="load">
            <i class="fas fa-download"></i> {{messages.sl_l || '加载'}}
          </button>
        </div>
        <div class="log-section">
          <div v-for="log in activeGame.logs" :key="log" class="log-item">
            <i class="fas fa-info-circle"></i> {{log}}
          </div>
        </div>
      </div>
      
      <div class="panel-glass welcome-panel" v-else>
        <h1 class="welcome-title"><i class="fas fa-handshake"></i> {{messages.sl_welcome || '欢迎使用SL管理'}}</h1>
      </div>
      
      <div class="modal-overlay" v-show="editor.show">
        <section class="modal-glass">
          <header class="modal-header">
            <i class="fas fa-edit"></i> {{ messages.sl_e || '编辑游戏' }}
          </header>
          <div class="modal-main">
            <div class="input-group">
              <label for="game" class="label-box">
                <div class="label-item"><i class="fas fa-gamepad"></i> {{messages.sl_gname || '游戏名称'}}</div>
                <div class="label-item error-msg">{{editor.errorMsg.gameMsg}}</div> 
              </label>
              <input type="text" name="game" placeholder="" v-model="editGameInfo.game" :disabled="editor.fromAdd == false" class="glass-input">
            </div>
            <div class="input-group">
              <label for="from" class="label-box">
                <div class="label-item"><i class="fas fa-folder"></i> {{messages.sl_location || '存档位置'}}</div>
                <div class="label-item error-msg">{{editor.errorMsg.locationMsg}}</div> 
                <div class="label-item checkbox-group">
                  <label for="selectDir" class="checkbox-label"><i class="fas fa-folder-open"></i> {{messages.sl_select_dir || '选择目录'}}:</label>
                  <input type="checkbox" name="selectDir" class="checkbox" v-model="editor.openDir">
                  <button class="select-btn" @click="selectFile">
                    <i class="fas fa-file-import"></i> {{messages.select_file || '选择文件'}}
                  </button>
                </div> 
              </label>
              <input type="input" name="from" placeholder="" v-model="editGameInfo.from" class="glass-input">
            </div>
          </div>
          <footer class="modal-footer">
            <button class="modal-btn cancel-btn" @click="editor.show= false">
              <i class="fas fa-times"></i> {{messages.cancel || '取消'}}
            </button>
            <button class="modal-btn save-btn" @click="editGame">
              <i class="fas fa-save"></i> {{messages.save || '保存'}}
            </button>
          </footer>
        </section>
      </div>

      <div class="add-button" @click="openAdd">
        <i class="fas fa-plus"></i>
      </div>
    </main>
  </div>
</template>

<style scoped>
.sl {
  display: flex;
  flex-direction: row;
  padding: 24px;
  height: 100vh;
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.8), rgba(30, 30, 40, 0.9));
  gap: 24px;
  box-sizing: border-box;
  font-family: 'Arial', sans-serif;
}

.gamelist-glass {
  box-sizing: border-box;
  width: 220px;
  min-height: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 24px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.sidebar-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #fff;
  text-align: center;
  margin: 0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.game-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.game-item {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
  transform: scale(1);
  text-align: center;
  padding: 0 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-item:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: scale(1.02);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.game-item:active {
  transform: scale(0.98);
  transition: transform 0.1s ease;
}

.game-active {
  background: rgba(169, 196, 194, 0.3);
  border: 1px solid rgba(169, 196, 194, 0.5);
  color: #fff;
  font-weight: 500;
}

.back-button {
  width: 100%;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  cursor: pointer;
  transition: all 0.3s ease;
  transform: scale(1);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  margin-top: auto;
  font-size: 1rem;
  font-weight: 500;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.back-button:hover {
  background: rgba(255, 255, 255, 0.25);
  border: 1px solid rgba(169, 196, 194, 0.4);
  transform: scale(1.02);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.back-button:active {
  transform: scale(0.98);
  transition: transform 0.1s ease;
}

.main-content {
  box-sizing: border-box;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  position: relative;
}

.panel-glass {
  width: 100%;
  height: 100%;
  min-height: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  padding: 32px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.15);
  animation: fadeIn 0.4s ease-out;
  box-sizing: border-box;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.welcome-panel {
  justify-content: center;
}

.welcome-title {
  font-size: 2rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.8);
  text-align: center;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.panel-title {
  font-size: 2rem;
  font-weight: 600;
  color: #fff;
  text-align: center;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.info-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.info-label {
  font-size: 1rem;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
  min-width: 120px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-label i {
  color: rgba(169, 196, 194, 0.8);
}

.info-value {
  font-size: 1rem;
  color: rgba(255, 255, 255, 0.8);
  word-break: break-all;
}

.action-buttons {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 24px 0;
}

.action-btn {
  width: 140px;
  height: 48px;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  transform: scale(1);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #fff;
}

.edit-btn {
  background: rgba(100, 150, 255, 0.3);
  border: 1px solid rgba(100, 150, 255, 0.4);
}

.save-btn {
  background: rgba(76, 175, 80, 0.3);
  border: 1px solid rgba(76, 175, 80, 0.4);
}

.load-btn {
  background: rgba(255, 152, 0, 0.3);
  border: 1px solid rgba(255, 152, 0, 0.4);
}

.action-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.action-btn:active {
  transform: scale(0.98);
  transition: transform 0.1s ease;
}

.log-section {
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.log-item {
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.8);
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  gap: 8px;
}

.log-item i {
  color: rgba(169, 196, 194, 0.8);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.modal-glass {
  width: 500px;
  display: flex;
  flex-direction: column;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.2);
  overflow: hidden;
  animation: slideIn 0.4s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-50px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-header {
  padding: 24px;
  font-size: 1.5rem;
  font-weight: 600;
  color: #fff;
  text-align: center;
  background: rgba(255, 255, 255, 0.1);
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.modal-main {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.label-box {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  align-items: center;
}

.label-item {
  display: flex;
  align-items: center;
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.9);
  gap: 6px;
}

.label-item i {
  color: rgba(169, 196, 194, 0.8);
}

.error-msg {
  color: #ff6b6b;
  font-size: 0.8rem;
  font-weight: 500;
}

.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
}

.checkbox-label {
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  gap: 6px;
}

.checkbox-label i {
  color: rgba(169, 196, 194, 0.8);
}

.checkbox {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.select-btn {
  height: 32px;
  padding: 0 16px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.select-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(169, 196, 194, 0.5);
}

.glass-input {
  width: 100%;
  height: 40px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 1rem;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: all 0.3s ease;
}

.glass-input:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(169, 196, 194, 0.5);
}

.glass-input:focus {
  outline: none;
  border-color: rgba(169, 196, 194, 0.8);
  box-shadow: 0 0 0 2px rgba(169, 196, 194, 0.3);
}

.modal-footer {
  padding: 24px;
  display: flex;
  justify-content: center;
  gap: 24px;
  background: rgba(255, 255, 255, 0.05);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-btn {
  width: 120px;
  height: 40px;
  border-radius: 10px;
  border: none;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  transform: scale(1);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #fff;
}

.cancel-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.save-btn {
  background: rgba(76, 175, 80, 0.3);
  border: 1px solid rgba(76, 175, 80, 0.4);
}

.modal-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.modal-btn:active {
  transform: scale(0.98);
  transition: transform 0.1s ease;
}

.add-button {
  position: fixed;
  bottom: 32px;
  right: 32px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(169, 196, 194, 0.3);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 2px solid rgba(255, 255, 255, 0.3);
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  z-index: 100;
  font-size: 24px;
  color: #fff;
}

.add-button:hover {
  background: rgba(169, 196, 194, 0.5);
  transform: scale(1.1);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
}

.add-button:active {
  transform: scale(0.95);
  transition: transform 0.1s ease;
}

/* 隐藏滚动条 */
.log-section::-webkit-scrollbar {
  display: none;
}

.log-section {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

/* FontAwesome 图标样式优化 */
.fas {
  font-size: 16px;
}

.info-label .fas {
  font-size: 14px;
}

.action-btn .fas {
  font-size: 16px;
}

.modal-btn .fas {
  font-size: 14px;
}

.select-btn .fas {
  font-size: 12px;
}

.add-button .fas {
  font-size: 24px;
}
</style>